package com.example.housemanage.add

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.housemanage.R
import android.widget.Button
import android.widget.Switch
import android.widget.Toast
import com.google.android.material.textfield.TextInputEditText
import androidx.activity.viewModels
import com.example.housemanage.entity.HouseManageEntityItem
import com.example.housemanage.HouseManageViewModel
import dagger.hilt.android.AndroidEntryPoint
import com.example.housemanage.databinding.ActivityAddHouseBinding
import com.example.lib_base.mvvm.BaseActivity

import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils

@AndroidEntryPoint
@Route(path = "/housemanage/add/AddHouseActivity")
class AddHouseActivity : BaseActivity<ActivityAddHouseBinding, HouseManageViewModel>() {
    override fun initView() {
        val etArea = binding.etArea
        val etEnterpriseId = binding.etEnterpriseId
        val etEnterpriseName = binding.etEnterpriseName
        val etRegionId = binding.etRegionId
        val etRegionName = binding.etRegionName
        val etRentDate = binding.etRentDate
        val switchRented = binding.switchRented
        val btnSubmit = binding.btnSubmit

        btnSubmit.setOnClickListener {
            val area = etArea.text.toString().toIntOrNull() ?: 0
            val enterpriseId = etEnterpriseId.text.toString().toIntOrNull() ?: 0
            val enterpriseName = etEnterpriseName.text.toString().trim()
            val regionId = etRegionId.text.toString().toIntOrNull() ?: 0
            val regionName = etRegionName.text.toString().trim()
            val rentDate = etRentDate.text.toString().trim()
            val rented = switchRented.isChecked

            if (area <= 0 || enterpriseId <= 0 || enterpriseName.isEmpty() || regionId <= 0 || regionName.isEmpty() || rentDate.isEmpty()) {
                ToastUtils.showLong("请填写完整信息")
                return@setOnClickListener
            }

            val entity = HouseManageEntityItem(
                area = area,
                enterpriseId = enterpriseId,
                enterpriseName = enterpriseName,
                id = 0, // 新增时id可为0或后端自增
                regionId = regionId,
                regionName = regionName,
                rentDate = rentDate,
                rented = rented
            )
            vm.addProperty(entity)
        }
    }

    override fun initData() {
        vm.addPropertyResult.observe(this) { success ->
            if (success) {
                ToastUtils.showLong("添加成功")
                finish()
            } else {
                ToastUtils.showLong("添加失败，请重试")
            }
        }
    }
    //防止内存泄漏
    override fun onDestroy() {
        super.onDestroy()
        vm.deletePropertyResult.removeObservers(this)
        vm.propertiesResult.removeObservers(this)
        vm.addPropertyResult.removeObservers(this)
        vm.updatePropertyResult.removeObservers(this)

    }
}